package com.ala4.oxcafe.service.impl;

import com.ala4.oxcafe.boot.manager.CustomUserDetails;
import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.boot.utils.DetailsHelper;
import com.ala4.oxcafe.common.utils.collection.CollUtil;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.ServiceOrderDetail;
import com.ala4.oxcafe.entity.WarrantyOrder;
import com.ala4.oxcafe.exception.BusinessException;
import com.ala4.oxcafe.repository.ServiceOrderDetailRepository;
import com.ala4.oxcafe.repository.WarrantyOrderRepository;
import com.ala4.oxcafe.service.WarrantyOrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 质保单
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/15 17:33
 */
@Slf4j
@Service
@AllArgsConstructor
public class WarrantyOrderServiceImpl implements WarrantyOrderService {

    private final WarrantyOrderRepository warrantyOrderRepository;

    private final ServiceOrderDetailRepository serviceOrderDetailRepository;

    @Override
    public IPage<WarrantyOrder> warrantyOrderPage(PageDomain pageDomain, WarrantyOrder warrantyOrder) {
        return warrantyOrderRepository.selectWarrantyOrderPage(pageDomain, warrantyOrder);
    }

    @Override
    public WarrantyOrder warrantyOrderInfo(Long warrantyId) {
        WarrantyOrder warrantyOrder = warrantyOrderRepository.getById(warrantyId);
        if (ObjectUtil.isNull(warrantyOrder)) {
            throw new BusinessException("找不到质保单信息");
        }
        List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderDetailRepository.list(Wrappers.<ServiceOrderDetail>lambdaQuery().eq(ServiceOrderDetail::getWarrantyId, warrantyId));
        warrantyOrder.setServiceOrderDetailList(serviceOrderDetailList);
        return warrantyOrder;
    }

    @Override
    public List<WarrantyOrder> weChatUserAll() {
        CustomUserDetails userDetails = DetailsHelper.getUserDetails();
        if (StrUtil.isEmpty(userDetails.getPhone())) {
            throw new BusinessException("用户未绑定手机号,无法查询");
        }
        return publicWarrantyUserPhone(userDetails.getPhone());
    }

    @Override
    public List<WarrantyOrder> publicWarrantyUserPhone(String phone) {
        List<WarrantyOrder> warrantyOrderList = warrantyOrderRepository.list(Wrappers.<WarrantyOrder>lambdaQuery().eq(WarrantyOrder::getCustomerPhone, phone));
        if (CollUtil.isNotEmpty(warrantyOrderList)) {
            List<Long> warrantyOrderIds = warrantyOrderList.stream().map(WarrantyOrder::getId).toList();

            // 查询全部的质保单明细
            List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderDetailRepository.list(Wrappers.<ServiceOrderDetail>lambdaQuery().in(ServiceOrderDetail::getWarrantyId, warrantyOrderIds));
            Map<Long, List<ServiceOrderDetail>> detailGroupMap = serviceOrderDetailList.stream().collect(Collectors.groupingBy(ServiceOrderDetail::getWarrantyId));

            for (WarrantyOrder warrantyOrder : warrantyOrderList) {
                warrantyOrder.setServiceOrderDetailList(detailGroupMap.get(warrantyOrder.getId()));
            }
        }

        return warrantyOrderList;
    }
}
